package com.sys.controller;

import com.alibaba.fastjson.JSONObject;
import com.sys.entity.SysRoleMenu;
import com.sys.service.SysRoleMenuService;
import com.bhudy.base.QueryFilter;
import com.bhudy.exception.SysException;
import com.bhudy.exception.SysExceptionCode;
import com.bhudy.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author bhudy
 * @Description 角色关联菜单controller
 * @Date 2019-10-30 23:09:37
 */
@RestController
public class SysRoleMenuController {

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    /**
     * 获取sysRole的数据
     *
     * @param roleId
     * @return sysRole的list
     */
    @GetMapping(value = "/api/v1/sysRoleMenus")
    public ResponseResult getAll(Long roleId) {
        if (roleId == null) throw new SysException(SysExceptionCode.CODE_40003);
        QueryFilter queryFilter = new QueryFilter();
        return ResponseResult.printMapJson(0, sysRoleMenuService.count(queryFilter), sysRoleMenuService.getTree(roleId, 0));
    }

    @GetMapping(value = "/api/v1/sysRoleMenu")
    public ResponseResult get(Long id) {
        return ResponseResult.printMapJson(0, sysRoleMenuService.get(id));
    }

    @PostMapping(value = "/api/v1/sysRoleMenus")
    public ResponseResult insert(@RequestBody String json, Long roleId) {
        if (roleId == null) throw new SysException(SysExceptionCode.CODE_40003);
        return ResponseResult.printMapJson(0, sysRoleMenuService.createSysRoleMenus(JSONObject.parseArray(json, SysRoleMenu.class), roleId));
    }

    @PutMapping(value = "/api/v1/sysRoleMenu")
    public ResponseResult update(SysRoleMenu sysRoleMenu) {
        return ResponseResult.printMapJson(0, sysRoleMenuService.update(sysRoleMenu));
    }

    @DeleteMapping(value = "/api/v1/sysRoleMenus")
    public ResponseResult delete(String id) {
        return ResponseResult.printMapJson(0, sysRoleMenuService.delete(id));
    }

}
